package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.NewsDaoImpl;
import com.ysd.entity.NewsClassify;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminNewsCategoryServlet extends BaseServlet {
	
	/**
	 * 获取新闻分类
	 * localhost:8080/shangcheng1/adminNewsCategory?reqName=selNewsCate
	 */
	 
	public void selNewsCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsDaoImpl newDao = new NewsDaoImpl();
		List<NewsClassify> fcList = newDao.getNews();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();//创建out对象
		
		Gson gson = new Gson();//创建gson对象
		
		String res = gson.toJson(fcList.size());//将fcList转化为json对象
		out.print(res);//响应
		
		out.flush();
		out.close();
		
	}

	/**
	 * localhost:8080/shangcheng1/adminNewsCategory?reqName=selNewsCateByPage
	 */
	 
	public void selNewsCateByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsDaoImpl newDao = new NewsDaoImpl();

		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<NewsClassify> fcList = newDao.getNewsCateByPage(page, size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();//创建out对象
		
		Gson gson = new Gson();//创建gson对象
		
		String res = gson.toJson(fcList);//将fcList转化为json对象
		out.print(res);//响应
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据Id获取要删除的数据
	 * localhost:8080/shangcheng1/adminNewsCategory?reqName=delNewsCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delNewsCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取前台传来的Id
		int newsId = Integer.parseInt(request.getParameter("newsClassifyId"));
		NewsDaoImpl NDao = new NewsDaoImpl();
		int m = NDao.getdelNewsCatbyId(newsId);//所有咨询分类
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (m > 0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
		
	}
	/**
	 * 根据咨询名称添加数据
	 * localhost:8080/shangcheng1/adminNewsCategory?reqName=addNewsCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addNewsCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码集
		PrintWriter out = response.getWriter();//out对象
		NewsDaoImpl nsDao = new NewsDaoImpl();//实体类对象
		
		String newsClassifyName = request.getParameter("newsClassifyName");//接受前台的参数
		if(newsClassifyName!=null && !newsClassifyName.equals("")) {
			List<NewsClassify> nwList =  nsDao.getNewsWhereByName(newsClassifyName);
			System.out.println(nwList);
			if (nwList.size() == 0) {//没有查到,可以添加
				NewsClassify n = new NewsClassify(0, newsClassifyName);//对象
				System.out.println(n);
				
				int f = nsDao.addNewsCategory(n);//调用实体类
				
				if(f > 0) {//受影响行数,成功
					out.print(Result.toClient(true, "添加成功","" ));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
					out.print(Result.toClient(false, "分类名已存在", ""));
			}
			
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng1/adminNewsCategory?reqName=getNewsCategoryById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNewsCategoryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码集
		PrintWriter out = response.getWriter();//out对象
		
		NewsDaoImpl nsDao = new NewsDaoImpl();//实体类对象
		int cId = Integer.parseInt(request.getParameter("newsCateId"));
		NewsClassify nc = nsDao.getNewsCateById(cId);
		
		Gson gson = new Gson();
		
		String res = gson.toJson(nc);
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	
	/**
	 * localhost:8080/shangcheng1/adminNewsCategory?reqName=updateNewsCategoryById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateNewsCategoryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码集
		PrintWriter out = response.getWriter();//out对象
		
		NewsDaoImpl nsDao = new NewsDaoImpl();//实体类对象
		int newsCategoryId = Integer.parseInt(request.getParameter("newCaId"));
		String newsClassifyName = request.getParameter("newCaName");//接收参数
		
		if(newsClassifyName!=null && !newsClassifyName.equals("")) {
			List<NewsClassify> ncList = nsDao.getNewsWhereByName(newsClassifyName);//去除重复查询
			System.out.println(ncList);
			if (ncList.size()==0) {
				NewsClassify ns = new NewsClassify(newsCategoryId, newsClassifyName); 
				System.out.println(ns);
				int n = nsDao.updateNewsCateById(ns);
				
				if(n > 0) {
					out.print(Result.toClient(true, "修改成功", ""));
				} else {
					out.print(Result.toClient(false, "修改失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "分类已存在", ""));
			}
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 根据多条产品分类id删除产品
	 * localhost:8080/shangcheng1/adminNewsCategory?reqName=delNewsCateByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delNewsCateByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		NewsDaoImpl nDao = new NewsDaoImpl();
		String ids = request.getParameter("ids");
		int n = nDao.delNewCateByIds(ids);
		
				
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
			
			out.flush();
			out.close();

	}
	
}
